package fy.form.listen.set;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.LogFactory;

import fy.form.listen.bean.Node;
import fy.util.MyUtil;

public class OA_UAPAccountContrast {

	private static org.apache.commons.logging.Log log = LogFactory
			.getLog(OA_UAPAccountContrast.class);

	private static Map<String, String> oa_uap_accountContrast = new Hashtable<String, String>();
	private static Map<String, Node> uap_oa_accountContrast = new Hashtable<String, Node>();
	private static List<Node> oaAccountList = new ArrayList<Node>();
	private static Map<String, String> uap_accountMap = new Hashtable<String, String>();
	private static Map<String, String> oa_accountMap = new Hashtable<String, String>();

	private static Map<String, String> uap_accountKeyUapShortNameMap = new HashMap<String, String>();

	private static boolean initState = false;

	private static List<String> uapShortNameList = new ArrayList<String>();

	public static List<String> getUapShortNameList() {
		if (!initState)
			initAccountContrast();
		return uapShortNameList;
	}

	public static List<Node> getOaAccountList() {
		if (!initState)
			initAccountContrast();

		return oaAccountList;
	}

	public static void setOaAccountList(List<Node> oaAccountList) {
		OA_UAPAccountContrast.oaAccountList = oaAccountList;
	}

	public static String getUapAccountID(String oaAccountID) {
		if (!initState)
			initAccountContrast();
		return oa_uap_accountContrast.get(oaAccountID);
	}

	public static String getAccountNameByUapID(String uapAccountID) {
		if (!initState)
			initAccountContrast();
		return uap_accountMap.get(uapAccountID);
	}

	public static String getAccountNameByOAID(String oaAccountID) {
		if (!initState)
			initAccountContrast();
		return oa_accountMap.get(oaAccountID);
	}

	public static Node getOAAccountID(String uapAccountID) {
		if (!initState)
			initAccountContrast();
		return uap_oa_accountContrast.get(uapAccountID);
	}

	public static String getUapAccountIDByUAPShortName(
			String uapAccountShortName) {
		if (!initState)
			initAccountContrast();
		return uap_accountKeyUapShortNameMap.get(uapAccountShortName);
	}

	public static boolean initAccountContrast() {
		boolean success = false;
		String path = MyUtil.getRealPath(OA_UAPAccountContrast.class)
				+ "account.conf";
		BufferedReader br = null;
		try {
			br = MyUtil.createBufferedReader(path);

			String temp = null;
			while ((temp = br.readLine()) != null) {
				temp = temp.replaceAll("\"", "").replaceAll(" ", "");
				if (!"".equals(temp)) {
					String[] array = temp.split(",");
					if (array.length == 4) {
						oa_uap_accountContrast.put(array[0], array[2]);
						Node node = new Node();
						node.setId(Long.valueOf(array[0]));
						node.setText(array[1]);
						uap_oa_accountContrast.put(array[2], node);
						oaAccountList.add(node);
						uap_accountMap.put(array[2], array[1]);
						oa_accountMap.put(array[0], array[1]);

						uap_accountKeyUapShortNameMap.put(array[3], array[2]);
						uapShortNameList.add(array[3]);
						node = null;
					}
					array = null;
				}
			}
			initState = true;
			success = true;

		} catch (Exception e) {
			e.printStackTrace();
			log.error("读取OA和UAP的制造部对照表失败", e);
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return success;
	}

	public class OAAccount {

	}
}
